/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import blueprint4j.db.*;
import blueprint4j.utils.*;


import java.awt.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.sql.*;

public class FieldColor extends Field implements BindColorInterface {

	/**
	 */
	public FieldColor( String p_name, int p_field_type , String p_description, Entity entity ) {
		super( p_name,p_field_type, p_description, entity );
	}

	public Field newInstance(Entity entity) {
		return new FieldColor(getName(),getFieldType(),getDescription(),entity);
	}

	static public Color getColorFromString(String string_color){
		try {
			if (string_color == null || string_color.length() < 6) {
				string_color = "000000";
			}
			return new Color(
			          Integer.parseInt(string_color.substring(0, 2), 16),
			          Integer.parseInt(string_color.substring(2, 4), 16),
			          Integer.parseInt(string_color.substring(4, 6), 16)
			       );
		} catch (Exception exception) {
			Log.debug.out(exception);
			return getColorFromString(null);
		}
	}

	protected void setFieldValue( ResultSet result_set )
	throws DataException {
		try {
			try {
				setObject(getColorFromString(result_set.getString(getName())));
			} catch (NumberFormatException nfe) {
				Log.debug.out( nfe);
				setObject(Color.BLACK);
			}
		} catch ( SQLException se ) {
			throw new DataException( "Can not retrieve '" + getName() + "'" );
		}
	}

	public Color get() {
		return (Color) getObject();
	}

	public void set(Color value) {
		setObject(value);
	}

	/**
	 * Return data in a suitable SQL format
	 */
	public String getSQLValue()
	throws DataException {
		if (get() == null) {
			return "null";
		}
		return entity.getConnection().getDataBaseUtils().convertStringForSQL(toString());
	}

	/**
	 * public Object getSQLObject()
	 *
	 * gets the data object for this field as the database would expect it
	 */
	public Object getSQLObject(){
		if (get() == null) {
			return null;
		}
		return toString();
	}

	public String toString() {
		Color color=get();
		if (color==null){
			return null;
		}
		String r,g,b;
		for (r=Integer.toString(color.getRed(),16);r.length()<2;r="0"+r);
		for (g=Integer.toString(color.getGreen(),16);g.length()<2;g="0"+g);
		for (b=Integer.toString(color.getBlue(),16);b.length()<2;b="0"+b);
		return r+g+b;
	}

	/**
	 * An overloaded equals method that only accepts the correct type
	 */
	public boolean equals(String value) {
		return get().equals(value);
	}

	public Bindable getBindable() {
		return getEntity();
	}
	
	public Object getPreparedStatementObject() 
	throws DataException {
		if (getObject() == null) return null;
		return toString();
	}
	
	public String getSerializable() throws BindException{
		if (getObject() == null) {
			return "null";
		}
		return ""+get().getRGB();
	}
	
	public void setSerializable(String value) throws BindException{
		if ("null".equalsIgnoreCase(""+value)) {
			setObject(null);
		} else {
			set(new Color(Integer.parseInt(value)));
		}
	}
}
